package businesslogic.userBl;


import java.rmi.Naming;
import java.rmi.RMISecurityManager;

import po.userPO.UserPO;
import businesslogicservice.userblservice.UserBLService;
import dataservice.ResultMessage;
import dataservice.userDataService.UserDatabaseService;





public class User  implements UserBLService{
	private static final String ADDRESS_STRING="localhost";
	protected String UserID;
	
	final int minPasswordSize=6;
	final int maxPasswordSize=12;
	
	public User(String UserID){
		this.UserID=UserID;
	}

	@Override
	public String getUserName(String ID)throws Exception{
		String userName=null;
		if(System.getSecurityManager()==null)
            System.setSecurityManager(new RMISecurityManager());
		try{

			UserDatabaseService userDataService = (UserDatabaseService) Naming
					.lookup("rmi://"+ADDRESS_STRING+":8888/userData");
			UserPO findResult = userDataService.find(ID);
			userName = findResult.getName();
			return userName;
			
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}
		
	}
	
	@Override
	public ResultMessage login(String ID, char[] password) {
		if(System.getSecurityManager()==null)
            System.setSecurityManager(new RMISecurityManager());
		try{
			
			UserDatabaseService userDataService=(UserDatabaseService)Naming.lookup("rmi://"+ADDRESS_STRING+":8888/userData");
			UserPO findResult=userDataService.find(ID);
			if(findResult==null){
				return ResultMessage.INVALID_ID;
			}
			else{
				char[] rightPassword=findResult.getPassword().toCharArray();
				boolean isRightUser=true;
				
				if(rightPassword.length!=password.length)
					isRightUser=false;
				
				else
				{
					for(int i=0;i<rightPassword.length;i++)
						if(rightPassword[i]!=password[i])
							isRightUser=false;
				}
				if(isRightUser==true){
					return ResultMessage.LOGIN_SUCCESS;
				}else{
					return ResultMessage.LOGIN_FAIL;
				}
				
		}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}


	@Override
	public  ResultMessage changePassword(char[] oldPassword, char[] password1,char[] password2){
		if(System.getSecurityManager()==null)
            System.setSecurityManager(new RMISecurityManager());
		try{
			UserDatabaseService userDataService=(UserDatabaseService)Naming.lookup("rmi://"+ADDRESS_STRING+":8888/userData");
			UserPO findResult=userDataService.find(UserID);
			char[] findPassword=findResult.getPassword().toCharArray();
			System.out.println(oldPassword);
			System.out.println(findPassword);
			boolean isRightUser=true;
			if(findPassword.length!=oldPassword.length)
				isRightUser=false;
			else{
				for(int i=0;i<findPassword.length;i++){
					if(findPassword[i]!=oldPassword[i])
						isRightUser=false;
				}
			}
			
			if(isRightUser==true){
				if(password1.length>maxPasswordSize||password1.length<minPasswordSize){
					return ResultMessage.INVALID_PASSWORD;
				}
				
				boolean isMatched=true;
				if(password1.length!=password2.length)
					isMatched=false;
				else{
					for(int i=0;i<password1.length;i++)
						if(password1[i]!=password2[i])
							isMatched=false;
				}
				
				if(isMatched==true){
					System.out.println(String.valueOf(password1));
					System.out.println(new String(password1));
					
					if(userDataService.update(UserID,String.valueOf(password1))==ResultMessage.UPDATE_SUCCESS){
						
						
						System.out.println("+++++++++++++++++++++++++");
						return ResultMessage.MODIFY_SUCCESS;
					}
					
				}
				else {
					return ResultMessage.MODIFY_FAIL;
				}
			}
				return ResultMessage.NO_ACCESS;
	
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public int getUserType(String ID)throws Exception{
		int userType=-1;
		if(System.getSecurityManager()==null)
            System.setSecurityManager(new RMISecurityManager());
		try{

			UserDatabaseService userDataService = (UserDatabaseService) Naming
					.lookup("rmi://"+ADDRESS_STRING+":8888/userData");
			UserPO findResult = userDataService.find(ID);
			userType = findResult.getUserType();
			return userType;
			
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}
		
	}
		
	
}
